plotnine 0.13.1¶

  • https://plotnine.org/

Installation¶

  • !pip install plotnine
In [1]:
### Installation
#!pip install plotnine
In [2]:
### For smoothing
#!pip install scikit-misc
#!pip install scikit-learn
  • Example
In [3]:
from plotnine import ggplot, geom_point, aes, stat_smooth, facet_wrap
from plotnine.data import mtcars

(
    ggplot(mtcars, aes("wt", "mpg", color="factor(gear)"))
    + geom_point()
    + stat_smooth(method="lm")
    + facet_wrap("gear")
)

Packages¶

In [4]:
import pandas as pd
import numpy as np
from plotnine import *

Data - mpg¶

In [5]:
from plotnine.data import mpg
mpg
Out[5]:
manufacturer model displ year cyl trans drv cty hwy fl class
0 audi a4 1.8 1999 4 auto(l5) f 18 29 p compact
1 audi a4 1.8 1999 4 manual(m5) f 21 29 p compact
2 audi a4 2.0 2008 4 manual(m6) f 20 31 p compact
3 audi a4 2.0 2008 4 auto(av) f 21 30 p compact
4 audi a4 2.8 1999 6 auto(l5) f 16 26 p compact
... ... ... ... ... ... ... ... ... ... ... ...
229 volkswagen passat 2.0 2008 4 auto(s6) f 19 28 p midsize
230 volkswagen passat 2.0 2008 4 manual(m6) f 21 29 p midsize
231 volkswagen passat 2.8 1999 6 auto(l5) f 16 26 p midsize
232 volkswagen passat 2.8 1999 6 manual(m5) f 18 26 p midsize
233 volkswagen passat 3.6 2008 6 auto(s6) f 17 26 p midsize

234 rows × 11 columns

In [6]:
### Read csv file
data_raw = pd.read_csv("https://vincentarelbundock.github.io/Rdatasets/csv/ggplot2/mpg.csv")
In [7]:
data_raw.shape
Out[7]:
(234, 12)
In [8]:
data_raw.head()
Out[8]:
rownames manufacturer model displ year cyl trans drv cty hwy fl class
0 1 audi a4 1.8 1999 4 auto(l5) f 18 29 p compact
1 2 audi a4 1.8 1999 4 manual(m5) f 21 29 p compact
2 3 audi a4 2.0 2008 4 manual(m6) f 20 31 p compact
3 4 audi a4 2.0 2008 4 auto(av) f 21 30 p compact
4 5 audi a4 2.8 1999 6 auto(l5) f 16 26 p compact
In [9]:
data_raw.describe(include='all')
Out[9]:
rownames manufacturer model displ year cyl trans drv cty hwy fl class
count 234.000000 234 234 234.000000 234.000000 234.000000 234 234 234.000000 234.000000 234 234
unique NaN 15 38 NaN NaN NaN 10 3 NaN NaN 5 7
top NaN dodge caravan 2wd NaN NaN NaN auto(l4) f NaN NaN r suv
freq NaN 37 11 NaN NaN NaN 83 106 NaN NaN 168 62
mean 117.500000 NaN NaN 3.471795 2003.500000 5.888889 NaN NaN 16.858974 23.440171 NaN NaN
std 67.694165 NaN NaN 1.291959 4.509646 1.611534 NaN NaN 4.255946 5.954643 NaN NaN
min 1.000000 NaN NaN 1.600000 1999.000000 4.000000 NaN NaN 9.000000 12.000000 NaN NaN
25% 59.250000 NaN NaN 2.400000 1999.000000 4.000000 NaN NaN 14.000000 18.000000 NaN NaN
50% 117.500000 NaN NaN 3.300000 2003.500000 6.000000 NaN NaN 17.000000 24.000000 NaN NaN
75% 175.750000 NaN NaN 4.600000 2008.000000 8.000000 NaN NaN 19.000000 27.000000 NaN NaN
max 234.000000 NaN NaN 7.000000 2008.000000 8.000000 NaN NaN 35.000000 44.000000 NaN NaN

Remove first column¶

In [10]:
data_use = data_raw.copy()
data_use = data_use.iloc[:,1:]
data_use
Out[10]:
manufacturer model displ year cyl trans drv cty hwy fl class
0 audi a4 1.8 1999 4 auto(l5) f 18 29 p compact
1 audi a4 1.8 1999 4 manual(m5) f 21 29 p compact
2 audi a4 2.0 2008 4 manual(m6) f 20 31 p compact
3 audi a4 2.0 2008 4 auto(av) f 21 30 p compact
4 audi a4 2.8 1999 6 auto(l5) f 16 26 p compact
... ... ... ... ... ... ... ... ... ... ... ...
229 volkswagen passat 2.0 2008 4 auto(s6) f 19 28 p midsize
230 volkswagen passat 2.0 2008 4 manual(m6) f 21 29 p midsize
231 volkswagen passat 2.8 1999 6 auto(l5) f 16 26 p midsize
232 volkswagen passat 2.8 1999 6 manual(m5) f 18 26 p midsize
233 volkswagen passat 3.6 2008 6 auto(s6) f 17 26 p midsize

234 rows × 11 columns

ggplot: 시각화 자료 + 시각화 특성¶

In [11]:
ggplot(data_use, aes(x='displ', y='hwy'))

ggplot: 시각화 자료 + 시각화 특성 + 시각화 종류¶

In [12]:
ggplot(data_use, aes(x='displ', y='hwy')) + geom_point()
In [13]:
ggplot(data_use) + geom_point(aes(x='displ', y='hwy'))
In [14]:
ggplot() + geom_point(aes(x='displ', y='hwy'), data=data_use)

Smooth line & SE¶

In [15]:
ggplot(data_use, aes(x='displ', y='hwy')) + geom_smooth()

Point + Smooth line¶

In [16]:
(
    ggplot(data_use, aes(x='displ', y='hwy')) + 
    geom_point() +
    geom_smooth()
)